// 分配权限
function jurisdiction_main() {
    return $.layout({
        id: 'basciPower_main_layout',
        items: [{
            groupField: 'roleType',
            features: [{
                padding: '5px',
                groupHeaderTpl: "分组：<b  style='color:red'>{[ DICS['roleType'][values.groupValue] ]}</b> 共{rows.length}条记录",
                ftype: 'grouping'
            }],
            order: ['roleType->asc', 'roleSort->asc'],
            region: 'west',
            xtype: 'jgrid',
            clazz: BasicRole,
            id: 'basciPower_role_west',
            where: [['roleType', '!=', '4'], ['organId.id', '=', USER.areaId]],
            top: [{
                xtype: 'textfield',
                name: 'roleName',
                op: 'like',
                text: '&nbsp;',
                labelWidth: 0,
                emptyText: '检索角色名称'
            }, $.search],
            columns: [{
                width: 120,
                name: 'roleType',
                resizable: false,
                renderer: $.dicRenderer('roleType')
            }, {
                resizable: false,
                flex: 1,
                name: 'roleName',
                renderer: function (v, t) {
                    t.tdCls = 'td-gray';
                    return v;
                }
            }],
            width: 350,
            style: $.border('Right'),
            listeners: {
                select: function (cmp, row) {
                    var basicModel_main = $('basicModel_main');
                    // 设置模块值
                    $.post('basciPower/listRoleModel.j', {
                        roleId: row.get('id')
                    }, function (result) {
                        var data = result == '' ? [] : result.split(',');
                        basicModel_main.clearCheckedNodes().setCheckedNodes(data).setTitle("正在分配【" + row.get('roleName')
                            + "】的模块");
                    }, false);
                }
            }
        }, {
            caseType: 'all',
            ckModel: 'all',
            xtype: 'jtree',
            clazz: BasicModel,
            id: 'basicModel_main',
            region: 'center',
            mapper: 'id,modelName,modelParentId.id,modelSort,modelJs,modelMain,modelIcon,modelType,iconCls',
            order: ['modelSort->asc'],
            rule: [['id', 'in', 'modelIds']],
            header: {
                iconCls: 'fa-wrench',
                style: {
                    backgroundColor: '#2C3845'
                },
                title: '角色模块设置'
            },
            listeners: {
                cellcontextmenu: function (cmp, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                    e.preventDefault();
                    e.stopEvent();
                    if (record.isLeaf()) {
                        record.set('checked', !record.get('checked'));
                    }
                }
            },
            top: [$.treeSearch, '->', {
                text: '保存权限设置',
                key: 'S',
                cls: 'blue',
                handler: function () {
                    basciPowerSave();
                }
            }],
            columns: [{
                flex: 1,
                text: '模块名称',
                name: 'text'
            }, {
                width: 200,
                name: 'modelType',
                renderer: $.dicRenderer('modelType', function (v, m) {
                    m.tdCls = 'td-blue';
                })
            }]
        }]
    })
}

// 保存设置
function basciPowerSave() {
    var data = [], sel = $('basciPower_role_west').select()[0];
    if (!sel) {
        return $.error('尚未操作任何角色!');
    }
    var roleId = sel.get('id');
    data = $('basicModel_main').getCheckedIds();
    // if (data.length == 0) {
    // return $.error('尚未操分配任何模块!');
    // }

    $.orm.mtm({
        clazz: BasicRoleModel,
        src: 'roleId',
        srcId: roleId,
        infos: [{
            tagerField: 'modelId',
            ids: data.join(',')
        }],
        callback: function () {
            $.show('模块分配成功.')
        }
    });

}
